/* eslint-disable no-nested-ternary */
import moment from 'moment';
import {uuid} from '../utils';
export const addMonths = (n, time) => {
    const v = time ? new Date(time) : new Date();
    if(v) {
        const t = new Date(v.getFullYear(), v.getMonth(), v.getDate(), v.getHours(), v.getMinutes(), v.getSeconds(), v.getMilliseconds());
        return moment(t.setMonth(v.getMonth() - (n + 1))).format('Y-MM');
    }
};

export const polylinelist = (n, data, y) => {
    const box = [];
    if(data.length > 0)
        // eslint-disable-next-line array-callback-return
        data.map(item => {
            const list = n === 1 ? [{
                id: uuid(),
                month: addMonths(10, y),
                value: item.n1MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(9, y),
                value: item.n2MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(8, y),
                value: item.n3MonthNumber,
                year: item.dealerCode.toString()
            }, {
                month: addMonths(7, y),
                value: item.n4MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(6, y),
                value: item.n5MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(5, y),
                value: item.n6MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(4, y),
                value: item.n7MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(3, y),
                value: item.n8MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(2, y),
                value: item.n9MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(1, y),
                value: item.n10MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n11MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n12MonthNumber,
                year: item.dealerCode.toString()
            }] : n === 2 ? [{
                id: uuid(),
                month: addMonths(4, y),
                value: item.n1MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(3, y),
                value: item.n2MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(2, y),
                value: item.n3MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(1, y),
                value: item.n4MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n5MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n6MonthNumber,
                year: item.dealerCode.toString()
            }] : [{
                id: uuid(),
                month: addMonths(1, y),
                value: item.n1MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n2MonthNumber,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n3MonthNumber,
                year: item.dealerCode.toString()
            }];
            box.push(...list);
        });
    return box;
};
